package com.herui.iot.data.report.source;

import com.alibaba.fastjson.TypeReference;
import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sdyu_lib.utils.JsonUtils;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sport_care.data.BaseCallBack;
import com.herui.sport_care.data.BaseRepository;
import com.herui.sport_care.data.report.EvaluatorAppEvaluationReportResponse;
import com.herui.sport_care.data.report.EvaluatorAppQueryEvaluationReportSingleResp;
import com.herui.sport_care.data.report.Report;
import com.herui.sport_care.data.report.ReportUser;
import com.herui.sport_care.data.report.TrendData;
import com.herui.sport_care.data.user.UserInfo;
import com.herui.sport_care.request.ReportRequest;
import com.herui.sport_care.request.SportPageResponse;
import com.herui.sport_care.request.SportResponse;

import java.util.List;
import java.util.Map;

import static com.herui.sport_care.config.ServerConfig.retrofitCall;

/**
 * Created by amanda.shan on 2021/9/30.
 */
public class ReportRepository extends BaseRepository implements ReportSource {

    private static ReportRepository instance;

    public static ReportRepository getInstance() {
        if (instance == null) {
            synchronized (ReportRepository.class) {
                if (instance == null) {
                    instance = new ReportRepository();
                }
            }
        }
        return instance;
    }

    private UserInfo userInfo;

    public void clearAll() {

    }

    @Override
    public void getReportList(Map<String, Object> map,
                              Callback.CommonCallback<List<Report>> callback) {
        retrofitCall.call(retrofitCall.conver(ReportRequest.class).getReportList(map),
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        LogUtils.d(result);
                        SportPageResponse<List<Report>> apiResponse;
                        try {
                            apiResponse = JsonUtils.parseObject(result,
                                    new TypeReference<SportPageResponse<List<Report>>>() {
                                    });
                            ;
                            if (apiResponse.getSuccess()) {
                                List<Report> t =
                                        JsonUtils.parseObject(apiResponse.getResult().getContent().toString(),
                                                new TypeReference<List<Report>>() {
                                                });
                                apiResponse.getResult().setContent(t);
                                callback.onSuccess((List<Report>) apiResponse.getResult().getContent());
                            } else {
                                callback.onError(null, "%s", apiResponse.getMsg());
                            }
                        } catch (Exception e) {
                            callback.onError(e, "", "");
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable throwable, String s, String s1) {
                        callback.onError(throwable, s + ":%s", s1);
                    }
                });
    }

    @Override
    public void getReportUserList(Long evaluationSchemeId,
                                  Callback.CommonCallback<List<ReportUser>> callback) {
        retrofitCall.call(retrofitCall.conver(ReportRequest.class).getReportUserList(evaluationSchemeId),
                new BaseCallBack<List<ReportUser>>(callback) {
                    @Override
                    public SportResponse<List<ReportUser>> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<List<ReportUser>>>() {
                                });
                    }

                });
    }

    @Override
    public void getEvaluationReportSingle(Long id,
                                          Callback.CommonCallback<EvaluatorAppQueryEvaluationReportSingleResp> callback) {
        retrofitCall.call(retrofitCall.conver(ReportRequest.class).getEvaluationReportSingle(id),
                new BaseCallBack<EvaluatorAppQueryEvaluationReportSingleResp>(callback) {
                    @Override
                    public SportResponse<EvaluatorAppQueryEvaluationReportSingleResp> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<EvaluatorAppQueryEvaluationReportSingleResp>>() {
                                });
                    }

                });
    }

    @Override
    public void getEvaluationReportAll(Long id,
                                       Callback.CommonCallback<EvaluatorAppEvaluationReportResponse> callback) {
        retrofitCall.call(retrofitCall.conver(ReportRequest.class).getEvaluationReportAll(id),
                new BaseCallBack<EvaluatorAppEvaluationReportResponse>(callback) {
                    @Override
                    public SportResponse<EvaluatorAppEvaluationReportResponse> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<EvaluatorAppEvaluationReportResponse>>() {
                                });
                    }

                });
    }

    @Override
    public void getTrendData(Long betaDataId, Callback.CommonCallback<TrendData> callback) {
        retrofitCall.call(retrofitCall.conver(ReportRequest.class).getTrendData(betaDataId),
                new BaseCallBack<TrendData>(callback) {
                    @Override
                    public SportResponse<TrendData> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<TrendData>>() {
                                });
                    }

                });
    }

    @Override
    public void updateEvaluationReport(Map<String, Object> map,
                                       Callback.CommonCallback<String> callback) {
        retrofitCall.call(retrofitCall.conver(ReportRequest.class).updateEvaluationReport(map),
                new BaseCallBack<String>(callback) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });
    }
}
